package com.tllq.zhongkang.controller;

import com.tllq.zhongkang.entity.UpdateCard;
import com.tllq.zhongkang.entity.ResultMap;
import com.tllq.zhongkang.service.UpdateCardService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UpdateCardController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UpdateCardService updateCardService;

    @GetMapping("/update-member")
    public ResultMap bindcard(UpdateCard updateCard) {
        ResultMap res = null;
        logger.info("/update-member传入参数：" + updateCard);
        if (StringUtils.isEmpty(updateCard.getCardNo()) || StringUtils.isEmpty(updateCard.getMobile())
                || StringUtils.isEmpty(updateCard.getBirthday()) || StringUtils.isEmpty(updateCard.getName())
        ) {

            res = ResultMap.error("参数不全");
            logger.error("/update-member返回数据：" + res);
            return res;

        }
        Integer byId = updateCardService.findById(updateCard.getCardNo());
        if (byId == 0) {
            res = ResultMap.error(updateCard.getCardNo()+"此卡不可用或已经启用");
            logger.error("/update-member返回数据：" + res);
            return res;
        }
        Integer bymobile = updateCardService.findBymobile(updateCard.getMobile());
        if (bymobile != 0) {
            res = ResultMap.error(updateCard.getMobile()+"手机号已存在请查看ERP中会员状态");
            logger.error("/update-member返回数据：" + res);
            return res;
        }
        try {
            updateCardService.update(updateCard);
            res = ResultMap.ok("更新成功");
            logger.info("/update-member返回数据{}", res);
        } catch (Exception e) {
            logger.error("发卡失败" + e.getMessage());
            res = ResultMap.error("发卡失败");
        }
        return res;
    }
}
